package com.ashes.brave.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.ashes.belife.common.exception.AshesException;
import com.ashes.belife.common.utils.ResultCodeEnum;
import com.ashes.brave.mapper.MsgSysMapper;
import com.ashes.brave.service.MsgSysService;
import com.ashes.oppo.po.SysAshesMsgPO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

/**
 * @author chengxuhui
 * @description 描述
 */
@Slf4j
@Service
public class MsgSysServiceImpl implements MsgSysService {

    @Autowired
    private MsgSysMapper msgSysMapper;

    @Override
    public Integer addMsgInfo(SysAshesMsgPO sysAshesMsgPO) {
        log.info("请求接口 addMsgInfo 入参{}",sysAshesMsgPO);
        sysAshesMsgPO.setMessageId(new BigDecimal(String.valueOf(IdUtil.getSnowflake().nextId())));
        sysAshesMsgPO.setCreateTime(DateUtil.now());
        Integer count = msgSysMapper.addMsgInfo(sysAshesMsgPO);
        if (count!=1){
            throw new RuntimeException("添加失败");
        }
        //统一记录结果集日志
        log.info("请求接口 addMsgInfo 返回结果{}",count);
        return count;
    }

    @Override
    public Integer updateMsgInfo(SysAshesMsgPO sysAshesMsgPO) {
        log.info("请求接口 updateMsgInfo 入参{}",sysAshesMsgPO);
        sysAshesMsgPO.setCreateTime(DateUtil.now());
        Integer count = msgSysMapper.updateMsgInfo(sysAshesMsgPO);
        if (count!=1){
            throw new RuntimeException("修改失败");
        }
        //统一记录结果集日志
        log.info("请求接口 updateMsgInfo 返回结果{}",count);
        return count;
    }

    @Override
    public Integer deleteMsgInfo(String messageId) {
        log.info("请求接口 deleteMsgInfo 入参{}",messageId);
        Integer count = msgSysMapper.deleteMsgInfo(messageId);
        if (count!=1){
            throw new RuntimeException("删除失败");
        }
        //统一记录结果集日志
        log.info("请求接口 deleteMsgInfo 返回结果{}",count);
        return count;
    }

    @Override
    public List<SysAshesMsgPO> queryMsgListData(SysAshesMsgPO sysAshesMsgPO) {
        log.info("请求接口 queryMsgListData 入参{}",sysAshesMsgPO);
        if (Objects.isNull(sysAshesMsgPO)){
            throw  new AshesException(ResultCodeEnum.ILLEGAL_REQUEST);
        }
        List<SysAshesMsgPO> msgList = msgSysMapper.queryMsgListData(sysAshesMsgPO);
//        msgList = msgList.stream().filter(item -> item.getUserId()!=null).collect(Collectors.toList());
        //统一记录结果集日志
        log.info("请求接口 queryMsgListData 返回结果{}",msgList);
        return msgList;
    }

    @Override
    public SysAshesMsgPO queryMsgInfoById(String id) {
        log.info("请求接口 queryMsgInfoById 入参{}",id);
        SysAshesMsgPO sysAshesMsgPO =  msgSysMapper.queryMsgInfoById(id);
        //统一记录结果集日志
        log.info("请求接口 queryMsgListData 返回结果{}",sysAshesMsgPO);
        return sysAshesMsgPO;
    }
}
